package com.android.place360.database;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class Favourite_DAO {

	public static boolean AddFavourite(Context context, Favourite favo) {
		boolean result = true;

		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
		
			mDB.addPlace(favo.getName(), favo.getAddress(), favo.getLat(), favo.getLng(), favo.getCatalog(), favo.getPhone());
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}

	public static ArrayList<Favourite> getAllFavourites(Context context) {
		ArrayList<Favourite> places = new ArrayList<Favourite>();
		DBAdapter mDB= new DBAdapter(context);
		try{
			 
		mDB.open();
		Cursor mCursor = mDB.getAllPlaces();
		/* retrieve data from database */

		int nId = mCursor.getColumnIndex(DBAdapter.P_ID);
		int nName = mCursor.getColumnIndex(DBAdapter.P_NAME);
		int nLat = mCursor.getColumnIndex(DBAdapter.P_LAT);
		int nLng = mCursor.getColumnIndex(DBAdapter.P_LNG);
		int nCatalog = mCursor.getColumnIndex(DBAdapter.P_CATALOG);
		int nPhone = mCursor.getColumnIndex(DBAdapter.P_PHONE);
		int nAddress = mCursor.getColumnIndex(DBAdapter.P_ADDRESS);

		// Check if our result was valid.
		mCursor.moveToFirst();
		if (mCursor != null) {

			// Loop through all Results
			do {
				Favourite fv = new Favourite();
				fv.setId(mCursor.getInt(nId));
				fv.setName(mCursor.getString(nName));
				fv.setLat(mCursor.getDouble(nLat));
				fv.setLng(mCursor.getDouble(nLng));
				fv.setCatalog(mCursor.getInt(nCatalog));
				fv.setPhone(mCursor.getString(nPhone));
				fv.setAddress(mCursor.getString(nAddress));
				places.add(fv);
			} while (mCursor.moveToNext());
			mCursor.close();
		}
		}catch (Exception e) {
			Log.i("error", e.getMessage());
		}
		finally{
			mDB.close();
		}
		return places;
	}
	
	public static ArrayList<Favourite> getFavouritesByCatalog(Context context, int catalog) {
		ArrayList<Favourite> places = new ArrayList<Favourite>();
		DBAdapter mDB= new DBAdapter(context);
		try{
			 
		mDB.open();
		Cursor mCursor = mDB.getPlacesByCatalog(catalog);
		/* retrieve data from database */

		int nId = mCursor.getColumnIndex(DBAdapter.P_ID);
		int nName = mCursor.getColumnIndex(DBAdapter.P_NAME);
		int nLat = mCursor.getColumnIndex(DBAdapter.P_LAT);
		int nLng = mCursor.getColumnIndex(DBAdapter.P_LNG);
		int nCatalog = mCursor.getColumnIndex(DBAdapter.P_CATALOG);
		int nPhone = mCursor.getColumnIndex(DBAdapter.P_PHONE);
		int nAddress = mCursor.getColumnIndex(DBAdapter.P_ADDRESS);

		// Check if our result was valid.
		mCursor.moveToFirst();
		if (mCursor != null) {

			// Loop through all Results
			do {
				Favourite fv = new Favourite();
				fv.setId(mCursor.getInt(nId));
				fv.setName(mCursor.getString(nName));
				fv.setLat(mCursor.getDouble(nLat));
				fv.setLng(mCursor.getDouble(nLng));
				fv.setCatalog(mCursor.getInt(nCatalog));
				fv.setPhone(mCursor.getString(nPhone));
				fv.setAddress(mCursor.getString(nAddress));
				places.add(fv);
			} while (mCursor.moveToNext());
			mCursor.close();
		}
		}catch (Exception e) {
			Log.i("error", e.getMessage());
		}
		finally{
			mDB.close();
		}
		return places;
	}
	
	
	public static Favourite getFavourite(Context context, int id) {
		Favourite fv = new Favourite();
		DBAdapter mDB= new DBAdapter(context);
		try{
			 
		mDB.open();
		Cursor mCursor = mDB.getPlaceByID(id);
		/* retrieve data from database */

		int nId = mCursor.getColumnIndex(DBAdapter.P_ID);
		int nName = mCursor.getColumnIndex(DBAdapter.P_NAME);
		int nLat = mCursor.getColumnIndex(DBAdapter.P_LAT);
		int nLng = mCursor.getColumnIndex(DBAdapter.P_LNG);
		int nCatalog = mCursor.getColumnIndex(DBAdapter.P_CATALOG);
		int nPhone = mCursor.getColumnIndex(DBAdapter.P_PHONE);
		int nAddress = mCursor.getColumnIndex(DBAdapter.P_ADDRESS);

		// Check if our result was valid.
		mCursor.moveToFirst();
		if (mCursor != null) {
				fv.setId(mCursor.getInt(nId));
				fv.setName(mCursor.getString(nName));
				fv.setLat(mCursor.getDouble(nLat));
				fv.setLng(mCursor.getDouble(nLng));
				fv.setCatalog(mCursor.getInt(nCatalog));
				fv.setPhone(mCursor.getString(nPhone));
				fv.setAddress(mCursor.getString(nAddress));
				
				mCursor.close();
		}
		mCursor.close();
		}catch (Exception e) {
			Log.i("error", e.getMessage());
		}
		finally{
			
			mDB.close();
		}
		return fv;
	}
	
	public static boolean deleteFavourite(Context context, int id) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.deletePlace(id);
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
	
	public static boolean deleteAllFavourite(Context context) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.deleteAllCatalog();
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
	
	public static boolean deleteAllFavouriteByCatalog(Context context, int catalogId) {
		boolean result = true;
		DBAdapter mDB = new DBAdapter(context);
		try {
			
			mDB.open();
			mDB.deletePlaceByCatalog(catalogId);
		} catch (Exception ex) {
			Log.i("error", "error " + ex.getMessage());
			result = false;
		}
		finally{
			mDB.close();
		}
		return result;
	}
	
}
